package com.liu.voluntary.controller;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.voluntary.mapper.AdminMapper;
import com.liu.voluntary.pojo.Admin;
import com.liu.voluntary.pojo.User;
import com.liu.voluntary.service.AdminService;
import com.liu.voluntary.service.UserService;
import com.liu.voluntary.util.MD5Util;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class AdminController {
    @Resource
    private AdminMapper adminMapper;


    @RequestMapping("/adminList")
    public String   adminList(Model model,@RequestParam(defaultValue = "1") int currentPage){

//    List<Admin> admins = adminMapper.selectList(null);

        int pageSize = 5;

        Page page = new Page<>(currentPage,pageSize);

        adminMapper.selectPage(page, null);
        List admins = page.getRecords();
        List<Admin> adminList = adminMapper.selectList(null);
        int number = adminList.size();
        int totalPages = (int) Math.ceil((double) number / pageSize);

        model.addAttribute("totalPages",totalPages);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("admins",admins);
   return "adminList";
    }

    @RequestMapping("/deleteAdmin/{id}")
    public String toDelete(Model model, @PathVariable("id") String id){

        int i = adminMapper.deleteById(id);
        if (i != 0){
            model.addAttribute("msg","删除成功");
        }

        return "redirect:/adminList";

    }



    @RequestMapping("/updateAdminPage/{id}")
    public String toUpdatePage(Model model, @PathVariable("id") String id){

        Admin admin = adminMapper.selectById(id);
        model.addAttribute("admin",admin);

        return "adminUpdate";

    }


    @RequestMapping("/updateAdmin/{id}")
    public String toUpdate(@RequestParam("nickname") String nickname,
                           @RequestParam("password") String password,
                           @PathVariable("id") String id,
                           Model model){

        Admin admin = new Admin();
        admin.setNickname(nickname);
        String dbPass = MD5Util.inputPassToDBPass(password);
        admin.setPassword(dbPass);

        UpdateWrapper<Admin> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);

        int i = adminMapper.update(admin, wrapper);

        if (i == 0){
            model.addAttribute("msg","更新失败");
        }

        return "redirect:/adminList";

    }


    @RequestMapping("/superAdmin")
    public String toSuper(){

        return "superAdmin";

    }




    //修改密码
    @RequestMapping("/updateAdminPwdPage")
    public String updatePwdPage(HttpServletRequest request, Model model){

        return "updateAdminPwdPage";

    }



    @Resource
    private AdminService adminService;
    @Resource
    private RedisTemplate redisTemplate;
    // 处理修改密码请求
    @PostMapping("/updateAdminPassword")
    public String updateUserPassword(@CookieValue("Ticket") String Ticket, @RequestParam(value = "oldPassword") String oldPassword, @RequestParam(value = "newPassword") String newPassword, Model model, HttpServletRequest request) {
        // 获取当前登录用户
        Admin admin =(Admin) redisTemplate.opsForValue().get("admin:" + Ticket);
//        Admin admin = (Admin) request.getSession().getAttribute(Ticket);
        if (admin == null) {
            return "redirect:/toLoginPage"; // 如果没有登录则重定向到登录页面
        }

        // 验证旧密码
        if (!admin.getPassword().equals(MD5Util.inputPassToDBPass(oldPassword))){
            return "redirect:/updateAdminPwdPage?error=true";
        }

        // 更新密码
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("password",MD5Util.inputPassToDBPass(newPassword))
                .eq("mobile",admin.getMobile());
        adminService.update(updateWrapper);




        // 设置成功消息
        model.addAttribute("success", true);

        return "redirect:/updateAdminPwdPage?success=true";
    }


    //个人信息页面
    @RequestMapping("/adminInfo")
    public String userInfo(@CookieValue("Ticket") String Ticket,Model model) {

        Admin admin = (Admin) redisTemplate.opsForValue().get("admin:" + Ticket);

        model.addAttribute("admin", admin);
        return "adminInfo";

    }


}
